package main


//http://localhost:8001/prome_http_sd 返回一堆key value
//通过该http接口可以获取代码设置的配置
import (
	"flag"
	"github.com/gin-gonic/gin"
	"math/rand"
)

func main() {

	listenAddress := flag.String("addr", ":8001",
		"Address on which to expose metrics and web interface.")
	flag.Parse()
	r := gin.Default()

	r.GET("/prome_http_sd", httpSd)
	r.Run(*listenAddress) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

type target struct {
	Targets []string          `json:"targets"`
	Labels  map[string]string `json:"labels"`
}

func httpSd(c *gin.Context) {

	nodes := []string{
		"172.20.70.205:9115",
		"http://prometheus.io",
		"http://www.baidu.com",
		"https://www.baidu.com",
		"https://github.com/",
	}
	randMapKeys := []string{"arch", "idc", "os", "ok"}
	randMapValues := []string{"linux", "beijing", "centos", "arm64"}
	frn := func(n int) int {
		return rand.Intn(n)
	}

	targets := make([]target, 0)
	for _, n := range nodes {
		num := len(randMapKeys)
		m := make(map[string]string, num)
		for i := 0; i < num; i++ {
			m[randMapKeys[frn(len(randMapKeys)-1)]] = randMapValues[frn(len(randMapValues)-1)]
		}
		t := target{
			Targets: []string{n},
			Labels:  m,
		}
		targets = append(targets, t)
	}

	c.JSON(200, targets)
}
